﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;

namespace BAL
{
    public partial class AbandonedDetail : Form
    {
        private LoginService.BarcodeJudgeServices bjs = new BAL.LoginService.BarcodeJudgeServices();
        Socket cliSocket = null;
        string ipAddr = "192.168.1.211";
        int port = 1024;

        private string PkgsID;
        private string FBillNo;
        private string FInterID;
        private string PkgsBarCode;
        private string PlaceBarCode;

        private string PlaceName;
        private string PkgsIsRegular;
        private string Fsku;
        private string StickLen;
        private string Stick;

        private string FQty;  
        private int Total;
        private int LeftQty;
        private string ry;
        private string Floor;     
        private string Fdate;
        private string ID;

        public AbandonedDetail()
        {
            InitializeComponent();
        }

        public AbandonedDetail(string fno, int num, string Fry,  string fdate)
        {
            InitializeComponent();
            FBillNo = fno;
            Total = num;
            ry = Fry;           
            Fdate = fdate;
        }

        public AbandonedDetail(string fno, int num, string Fry, string fdate,string _ID)
        {
            InitializeComponent();
            FBillNo = fno;
            Total = num;
            ry = Fry;
            Fdate = fdate;
            this.ID = _ID;
        }

        private void AbandonedDetail_Load(object sender, EventArgs e)
        {
            if (FBillNo == null || FBillNo == "")
            {
            }
            else
            {
                DataGridStyle();
                BindData();
                LeftQty = bjs.GetAbandonLeftQty(FBillNo, ry, Fdate);              
              
                this.tb_FBillNo.Text = FBillNo.Trim();
                this.tb_Total.Text = Total.ToString();                
                this.tb_LeftQty.Text = LeftQty.ToString();

                this.tb_PkgsBarCode.Focus();
                
            }
        }

        private void BindData()
        {
            DataSet ds = bjs.GetAbandonedDetail(FBillNo, ry,  Fdate);
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
            //if (dt.Rows.Count <= 0)
            //{
            //    return;
            //}
            //else
            //{
                dataGrid1.DataSource = dt;
            //}
        }

        public void DataGridStyle()
        {
            DataGridTableStyle dts = new DataGridTableStyle();
            dataGrid1.TableStyles.Clear();
            dts.MappingName = "AbandonedDetail";                   

            DataGridTextBoxColumn cl2 = new DataGridTextBoxColumn();
            cl2.HeaderText = "包装码";
            cl2.MappingName = "PkgsBarCode";
            cl2.Width = 50;
            dts.GridColumnStyles.Add(cl2);

            DataGridTextBoxColumn cl4 = new DataGridTextBoxColumn();
            cl4.HeaderText = "仓位名";
            cl4.MappingName = "PlaceName";
            cl4.Width = 120;
            dts.GridColumnStyles.Add(cl4);

            DataGridTextBoxColumn cl3 = new DataGridTextBoxColumn();
            cl3.HeaderText = "条码内码";
            cl3.MappingName = "PkgsID";
            cl3.Width = 50;
            dts.GridColumnStyles.Add(cl3);

            DataGridTextBoxColumn cl6 = new DataGridTextBoxColumn();
            cl6.HeaderText = "Fsku";
            cl6.MappingName = "FSku";
            cl6.Width = 150;
            dts.GridColumnStyles.Add(cl6);

            DataGridTextBoxColumn cl7 = new DataGridTextBoxColumn();
            cl7.HeaderText = "物料名";
            cl7.MappingName = "FItemName";
            cl7.Width = 110;
            dts.GridColumnStyles.Add(cl7);

            DataGridTextBoxColumn cl8 = new DataGridTextBoxColumn();
            cl8.HeaderText = "属性名";
            cl8.MappingName = "AttrValue";
            cl8.Width = 110;
            dts.GridColumnStyles.Add(cl8);                   

            DataGridTextBoxColumn cl9 = new DataGridTextBoxColumn();
            cl9.HeaderText = "根长";
            cl9.MappingName = "StickLen";
            cl9.Width = 30;
            dts.GridColumnStyles.Add(cl9);

            DataGridTextBoxColumn cl10 = new DataGridTextBoxColumn();
            cl10.HeaderText = "数量";
            cl10.MappingName = "FQty";
            cl10.Width = 30;
            dts.GridColumnStyles.Add(cl10);


            DataGridTextBoxColumn cl11 = new DataGridTextBoxColumn();
            cl11.HeaderText = "类别";
            cl11.MappingName = "PkgsIsRegular";
            cl11.Width = 30;
            dts.GridColumnStyles.Add(cl11);

            DataGridTextBoxColumn cl1 = new DataGridTextBoxColumn();
            cl1.HeaderText = "流水号";
            cl1.MappingName = "FInterID";
            cl1.Width = 40;
            dts.GridColumnStyles.Add(cl1);     

            dataGrid1.TableStyles.Add(dts);
        }

        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            DataGrid dataGrid1 = sender as DataGrid;
            int index = ((DataGrid)sender).CurrentCell.RowNumber;
            if (((DataTable)(dataGrid1.DataSource)).Rows.Count > 0)
            {
                ((DataGrid)sender).Select(index);
            }
            FInterID = ((DataTable)(dataGrid1.DataSource)).Rows[index]["FInterID"].ToString();
            PkgsBarCode = ((DataTable)(dataGrid1.DataSource)).Rows[index]["PkgsBarCode"].ToString();
            PlaceBarCode = ((DataTable)(dataGrid1.DataSource)).Rows[index]["PlaceBarCode"].ToString();
            PlaceName = ((DataTable)(dataGrid1.DataSource)).Rows[index]["PlaceName"].ToString();
            PkgsID = ((DataTable)(dataGrid1.DataSource)).Rows[index]["PkgsID"].ToString();
            this.tb_FBillNo.Text = FBillNo.Trim();
            this.tb_Total.Text = Total.ToString();
            this.tb_PkgsID.Text = PkgsID;
            this.tb_PkgsBarCode.Text = PkgsBarCode;
            this.tb_PlaceName.Text = PlaceName;        

            this.tb_PkgsBarCode.Focus();

        }

        private void tb_PkgsBarCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                 PkgsBarCode = this.tb_PkgsBarCode.Text;
                if (PkgsBarCode == null || PkgsBarCode == "")
                {
                    MessageBox.Show("请扫描包装码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    return;
                }
                else
                {
                    bool result = bjs.GetCode(PkgsBarCode, out  PlaceBarCode, out  PlaceName, out Floor, out  Fsku, out  StickLen, out  Stick, out  FQty, out  PkgsIsRegular);
                 
                     if (result)
                    {
                        this.tb_PlaceName.Text = PlaceName;
                        this.button3.Focus();
                    }
                    else
                    {
                        MessageBox.Show("没有该包装码！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                        return;
                    }
                }
            }
        }

        //返回
        private void button2_Click(object sender, EventArgs e)
        {
            Abandoned ad = new Abandoned(this.ID);
            ad.Show();
            this.Hide();
        }
        //提交
        private void button3_Click(object sender, EventArgs e)
        {
            PkgsBarCode = this.tb_PkgsBarCode.Text.Trim(); 
            PkgsID = this.tb_PkgsID.Text.Trim();
            if (PkgsBarCode == null || PkgsBarCode == "")
            {
                MessageBox.Show("请扫描包装码！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
            int li_return = bjs.UpdateAbandonedDetail(PkgsBarCode, PkgsID, Program.userid);
            if (li_return == 0)
            {
                MessageBox.Show("包装码错误或该包装码已经扫描过！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                return;
            }
            else if (li_return == 1) //成功
            { 
            
            }            
            LeftQty = bjs.GetAbandonLeftQty( FBillNo,  ry,   Fdate);   
            this.tb_LeftQty.Text = LeftQty.ToString();
            this.tb_PkgsID.Text = "";
            this.tb_PkgsBarCode.Text = "";
            this.tb_PlaceName.Text = "";
            this.tb_PkgsBarCode.Focus();
            DataGridStyle();
            BindData();

        }

      

        

      
          


    }
}